package interfaces.impl.agents;


import extra.Position;

import constants.*;
import interfaces.ActionInterface;
import interfaces.AgentInterface;
import interfaces.WorldInterface;
import interfaces.impl.Agent;
import interfaces.impl.actions.ActionModifySpetialFeature;
import interfaces.impl.actions.ActionXYtoMove;

import java.awt.Graphics;
import java.util.ArrayList;

import objects.SpetialFeature;
import objects.WorldObject;
import universe.Universe;

public class AgentNormalMove extends Agent {
	
	//attributes needed from object
		private double visionRange;
		private double speed;
		private double xObject;
		private double yObject;
		private double isMoving=0;
		
		private  double contTime=0;
		private  double delayTime=100;
		private ActionXYtoMove action;
		
	
	public AgentNormalMove(){
		this.id=Constants.AGENT_NORMAL_MOVE;
		
		listActions= new ArrayList<ActionInterface>();
		
		action = new ActionXYtoMove(priority);
		listActions.add(action);
		
	}

	@Override
	public void update(WorldObject object, Universe world,double time) {
		
		if(!canUpdate(object)){
			return;
		}
		int cont=0;
		for(SpetialFeature f : object.getListFeatures()){
			if(f.getId() == Constants.SPETIAL_FEATURE_SPEED){
				speed = f.getValue();
				cont++;
			}
			if(f.getId() == Constants.SPETIAL_FEATURE_X_TO_MOVE){
				xObject = f.getValue();
				cont++;
			}
			if(f.getId() == Constants.SPETIAL_FEATURE_Y_TO_MOVE){
				yObject = f.getValue();
				cont++;
			}
			if(f.getId() == Constants.SPETIAL_FEATURE_IS_MOVING){
				isMoving = f.getValue();
				cont++;
			}
			if(f.getId() == Constants.SPETIAL_FEATURE_VISION_RANGE){
				visionRange = f.getValue();
				//System.out.println("Mi vision range es "+visionRange);
				cont++;
			}
			
		}
		
		if(cont!=5){
			System.out.println("Me faltan atrubuts para poder hacer update Agent MOVE NORMAL");
			return;
		}
		
		contTime=contTime+time;
		if(contTime>=delayTime){
			contTime=delayTime;
		}
		
		if(isMoving==Constants.FALSE  && contTime==delayTime){
			//System.out.println("no me muevo");
			///i search a place to move 
	
				calculatePositionToMove((int)object.getPosition().getX(),(int) object.getPosition().getY(), world, object);
				contTime=0;
			
			
			
		}
		

	}

	@Override
	public void paint(Graphics g, WorldObject object) {
		return;
		
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "Mover Normal";
	}
	
	private void calculatePositionToMove(int x, int y, Universe world, WorldObject object){
		
		int x1;
		int y1;
		int count=0;
		
		
		do{
			x1 = (int) (((Math.random()*10000)%visionRange)-(visionRange/2)+(x));
			y1= (int) (((Math.random()*10000)%visionRange)-(visionRange/2) +(y));
			count++;
			if(x1<0){
				x1=0;
			}
			if(y1<0){
				y1=0;
			}
		}
		while(!world.getWorldI().isFree(new Position(x1,y1)) && count<5);
		 if(count<5){
			 action.setTarget(object);
			 action.setSource(object);
			 action.setxToMove(x1);
			 action.setyToMove(y1);
			 object.setImmediatlyActionToDo(action);
			 
		 }
	 
	}
	
	@Override
	public void setName(String name) {
		this.name=name;
		
	}


}
